#include <iostream>
#include <algorithm>
#include <set>

class Person
{
private:
    int age;
    std::string name;

public:
    Person(int age, std::string name) : name(name), age(age) {}
    int getAge() const
    {
        return age;
    }

    std::string getName() const
    {
        return name;
    }

    bool operator<(const Person &p) const
    {
        return age < p.age;
    }
};

std::ostream &operator<<(std::ostream &os, const Person &p)
{
    os << "{" << p.getName() << ", " << p.getAge() << "} ";
    return os;
}

void test01()
{
    std::vector<int> v{};
    for (int i = 0; i < 20; i++)
    {
        v.push_back(rand() % 100);
    }

    std::for_each(v.begin(), v.end(), [](const int &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;

    std::sort(v.begin(), v.end());

    std::for_each(v.begin(), v.end(), [](const int &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;

    std::sort(v.begin(), v.end(), [](const int &i, const int &j)
              { return i > j; });

    std::for_each(v.begin(), v.end(), [](const int &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;

    std::sort(v.begin(), v.end(), std::less<int>());
    std::for_each(v.begin(), v.end(), [](const int &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;
}

void test02()
{
    std::vector<Person> v{};
    for (int i = 0; i < 10; i++)
    {
        v.push_back(Person(rand() % 35, std::string("Person") + std::to_string(i + 1)));
    }
    std::for_each(v.begin(), v.end(), [](const Person &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;

    std::sort(v.begin(), v.end()); // 因为重载了 < 小于号这个操作符，或者加上谓词也行
    std::for_each(v.begin(), v.end(), [](const Person &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;

    std::sort(v.begin(), v.end(), [](const Person &p1, const Person &p2)
              { return p1.getAge() > p2.getAge(); });
    std::for_each(v.begin(), v.end(), [](const Person &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;
}

int main(int argc, char const *argv[])
{
    srand(time(0));
    test02();
    return 0;
}
